﻿/*********************************************************
 * 开发人员：小兵
 * 创建时间：2012-8-30 11:15:33
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
namespace Mozlite.Components
{
    using System;
    using System.Web;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;

    /// <summary>
    /// 文件设置。
    /// </summary>
    [Serializable]
    public class FileStorageSettings : BaseConfiguration<FileStorageSettings>
    {
        /// <summary>
        /// 初始化文件设置。
        /// </summary>
        public FileStorageSettings()
        {
            ImageFileExtensions = "gif,jpg,jpeg,png,bmp";
            FlashFileExtensions = "swf,flv";
            MediaFileExtensions = "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb";
            CommonFileExtensions = "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2";
            FileSize = 10240000;
            EnableCrossFileSystem = true;
        }
        /// <summary>
        /// 图片扩展名。
        /// </summary>
        public string ImageFileExtensions { get; set; }
        /// <summary>
        /// Flash文件扩展名。
        /// </summary>
        public string FlashFileExtensions { get; set; }
        /// <summary>
        /// 媒体文件扩展名。
        /// </summary>
        public string MediaFileExtensions { get; set; }
        /// <summary>
        /// 普通文件扩展名。
        /// </summary>
        public string CommonFileExtensions { get; set; }
        /// <summary>
        /// 单个文件大小。
        /// </summary>
        public long FileSize { get; set; }

        /// <summary>
        /// 判断是否允许。
        /// </summary>
        /// <param name="extension">文件扩展名。</param>
        /// <param name="type">文件类型。</param>
        /// <returns>返回是否允许上传。</returns>
        public bool IsAllowed(string extension, string type, out string allows)
        {
            if(type == null)
            {
                allows = ImageFileExtensions + "," + FlashFileExtensions + "," + MediaFileExtensions + "," + CommonFileExtensions;
            }
            else
            {
                switch(type.ToLower())
                {
                    case "image":
                        allows = ImageFileExtensions;
                        break;
                    case "flash":
                        allows = FlashFileExtensions;
                        break;
                    case "media":
                        allows = MediaFileExtensions;
                        break;
                    case "file":
                        allows = CommonFileExtensions;
                        break;
                    default:
                        allows = ImageFileExtensions + "," + FlashFileExtensions + "," + MediaFileExtensions + "," + CommonFileExtensions;
                        break;
                }
            }
            if(string.IsNullOrEmpty(allows) || allows.IndexOf("*") != -1)
            {
                allows = "*";
                return true;
            }
            allows = allows.ToLower().Split<string>(',').Distinct<string>().Join();
            return (allows + ',').IndexOf(extension.ToLower() + ",") != -1;
        }
        /// <summary>
        /// 判断是否为当前类型的文件。
        /// </summary>
        /// <param name="extension">文件扩展名。</param>
        /// <param name="type">文件类型。</param>
        /// <returns>返回是否当前类型的文件。</returns>
        public bool IsType(string extension, string type) {
            string allows;
            return IsAllowed(extension, type, out allows);
        }
        /// <summary>
        /// 是否开启全局文件系统，即所有人都可以看到磁盘文件。
        /// </summary>
        public bool EnableCrossFileSystem { get; set; }
    }
}
